package foodserver;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.json.JSONException;
import org.json.JSONObject;

import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.ClientDataRecordBase;
import com.subway.babynote.syncdata.PostParam;

import querysrc.query_change_user_birth_gender;
import querysrc.query_login;
import querysrc.sqlConnectionHandler;
import querysrc.sqlConnectionHandlerManager;
import serverdata.httpreqhandler;



public class Login extends addbase {
	private static final long serialVersionUID = 1L;
	
	query_login qa = null;
	
	protected void initialize_subquery()
	{
		qa = new query_login(this.sqlconhandler);
	}
	protected void destory_subquery()
	{
		this.release_query(qa);
	}
	 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	 {
			// TODO Auto-generated method stub
			 try
			 {		 
				 int iret = 0;
				 JSONObject jsono = this.read_data(request); 
					 //httpreqhandler.readInput(request);		 
				 ClientDataRecordBase.WRAPPER clr  = new ClientDataRecordBase.WRAPPER(null);
				 iret = this.login(jsono, clr);		 			 
				 
				 JSONObject return_value = new JSONObject();
				 return_value.put(PostParam.ReturnValue_Param.return_status, iret);
				 if(iret>=0)
				 {
					 return_value.put(PostParam.last_avgRecord_Param.records, clr.cdrb.toJSON());
				 }
				 //return_value.put(PostParam.ReturnValue_Param.err_message, errMessage.err_authenfail.errcode);
				 httpreqhandler.writeOutput(return_value, response);		     			 
			 }
			 catch (JSONException e) {
					// TODO Auto-generated catch block
					System.out.println("JSON error for FeedSync doPost:" + e.getMessage());
					e.printStackTrace();
					
				}
			 catch (Exception e) {
				 System.out.println("FeedSync doPost error:" + e.getMessage());
			      e.printStackTrace();
			    }
		}
	protected int login(JSONObject input_jsono, ClientDataRecordBase.WRAPPER clr)
	{
		try
		 {		
			 int iret = 0;
			 ClientDataRecord.usersRecord ur = new ClientDataRecord.usersRecord(input_jsono);
			 iret = qa.login(ur);
			 clr.cdrb = ur;
			 return iret;
		 }		 
		 catch (Exception e) {
			 System.out.println("FeedSync doPost error:" + e.getMessage());
		      e.printStackTrace();
		      return -1;
		    }
	}
	@Override
	protected int add_data(JSONObject input_jsono) {
		// TODO Auto-generated method stub
		return 0;
	}		
	 
}
